﻿

//#pragma execution_character_set("utf-8")
//#include "database.h"
//#include "orderwidget.h"
//#include "ui_orderwidget.h"

//OrderWidget::OrderWidget(QWidget *parent) :
//    QWidget(parent),
//    ui(new Ui::OrderWidget)
//{
//    ui->setupUi(this);
//    selectrow = 0;
//    QStringList header;
//    header <<"编号"<<"名称"<<"数量"<<"类型"<<"进货价"<<"指导价"<<"备注"<<"供应商";
//    //header <<"编号"<<"名称"<<"数量"<<"类型"<<"进货价"<<"指导价"<<"备注"<<"供应商"<<"质检状态";
//    ui->tableWidget->setColumnCount(9);
//    ui->tableWidget->setHorizontalHeaderLabels(header);
//    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
//    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
//    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
//    ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//    ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

//    ui->tableWidget->horizontalHeader()->setFixedHeight(50);

//    ui->tableWidget->verticalHeader()->setHidden(true);
//    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
//    ui->tableWidget->verticalHeader()->setDefaultSectionSize(50);
//    updateDisplay();
//    connect(ui->tableWidget, SIGNAL(cellClicked(int,int)),this,SLOT(onHeaderClicked(int,int)));
//}

//OrderWidget::~OrderWidget()
//{
//    delete ui;
//}

//void OrderWidget::showEvent(QShowEvent *event)
//{
//    updateDisplay();
//    // 每列占比你可根据实际情况调整（总共约1.0）
//    ui->tableWidget->horizontalHeader()->resizeSection(0,ui->tableWidget->width()*0.08); // 编号
//    ui->tableWidget->horizontalHeader()->resizeSection(1,ui->tableWidget->width()*0.1);  // 名称
//    ui->tableWidget->horizontalHeader()->resizeSection(2,ui->tableWidget->width()*0.08); // 数量
//    ui->tableWidget->horizontalHeader()->resizeSection(3,ui->tableWidget->width()*0.08); // 类型
//    ui->tableWidget->horizontalHeader()->resizeSection(4,ui->tableWidget->width()*0.1);  // 进货价
//    ui->tableWidget->horizontalHeader()->resizeSection(5,ui->tableWidget->width()*0.1);  // 指导价
//    ui->tableWidget->horizontalHeader()->resizeSection(6,ui->tableWidget->width()*0.18); // 备注
//    ui->tableWidget->horizontalHeader()->resizeSection(7,ui->tableWidget->width()*0.18); // 供应商
//   // ui->tableWidget->horizontalHeader()->resizeSection(8,ui->tableWidget->width()*0.1);  // 质检状态


//}

//void OrderWidget::updateDisplay()
//{
//    ui->tableWidget->clearContents();
//    QVector<QVector<QString>>  allinfo = DataBasePri::queryAll("orders");
//    ui->tableWidget->setRowCount(allinfo.size());
//    for(int i =0;i < allinfo.size();i++){
//        ui->tableWidget->setItem(i,0,new QTableWidgetItem(allinfo.at(i).at(0)));
//        ui->tableWidget->setItem(i,1,new QTableWidgetItem(allinfo.at(i).at(1)));
//        ui->tableWidget->setItem(i,2,new QTableWidgetItem(allinfo.at(i).at(2)));
//        ui->tableWidget->setItem(i,3,new QTableWidgetItem(allinfo.at(i).at(3)));
//        ui->tableWidget->setItem(i,4,new QTableWidgetItem(allinfo.at(i).at(4)));
//        ui->tableWidget->setItem(i,5,new QTableWidgetItem(allinfo.at(i).at(5)));
//        ui->tableWidget->setItem(i,6,new QTableWidgetItem(allinfo.at(i).at(6)));
//        ui->tableWidget->setItem(i,7,new QTableWidgetItem(allinfo.at(i).at(7)));
//        //ui->tableWidget->setItem(i,8,new QTableWidgetItem(allinfo.at(i).at(8))); // 质检状态
//    }
//}
////void OrderWidget::updateDisplay()
////{
////    ui->tableWidget->clearContents();

////    if (!DataBasePri::database.open()) {
////        qCritical() << DataBasePri::database.lastError().text();
////        return;
////    }

////    QSqlQuery query(DataBasePri::database);
////    QString sql = R"(
////        SELECT o.id, o.name, o.num, o.type, o.cost, o.price, o.remarks, o.supplier, q.status
////        FROM orders o
////        LEFT JOIN quality q ON o.id = q.order_id
////    )";

////    bool ret = query.exec(sql);
////    if (!ret) {
////        qCritical() << query.lastError().text();
////        return;
////    }

////    int row = 0;
////    ui->tableWidget->setRowCount(0);
////    while (query.next()) {
////        ui->tableWidget->insertRow(row);
////        for (int col = 0; col < 9; ++col) {
////            QString value = query.value(col).toString();
////            ui->tableWidget->setItem(row, col, new QTableWidgetItem(value));
////        }
////        row++;
////    }

////    DataBasePri::database.close();
////}


//void OrderWidget::onHeaderClicked(int row,int column)
//{
//    ui->lineEdit_a->setText(ui->tableWidget->item(row,0)->text());
//    ui->lineEdit_b->setText(ui->tableWidget->item(row,1)->text());
//    ui->lineEdit_c->setText(ui->tableWidget->item(row,2)->text());
//    ui->lineEdit_d->setText(ui->tableWidget->item(row,3)->text());
//    ui->lineEdit_e->setText(ui->tableWidget->item(row,4)->text());
//}

//void OrderWidget::on_pushButton_1_clicked()
//{
//    QString lineEdit_a = ui->lineEdit_a->text();//
//    QString lineEdit_b = ui->lineEdit_b->text();//
//    QString lineEdit_c = ui->lineEdit_c->text();//
//    QString lineEdit_d = ui->lineEdit_d->text();//
//    QString lineEdit_e = ui->lineEdit_e->text();//
//    QString lineEdit_f = ui->lineEdit_f->text();//
//    QString lineEdit_g = ui->lineEdit_g->text();//
//    QString lineEdit_h = ui->lineEdit_h->text();//
//    if(lineEdit_a == "" || lineEdit_b == ""){//判断是否为空
//        QMessageBox msgBox;
//        msgBox.setWindowTitle("提示");
//        msgBox.setText("用户名或密码不能为空");
//        msgBox.exec();
//    }else{
//        if (! DataBasePri::database.open()){//打开数据库
//            qCritical() << DataBasePri::database.lastError().text();
//            return;
//        }
//        QSqlQuery query(DataBasePri::database);//创建数据库请求对象
//        QString t_str = QString::fromLocal8Bit("insert into orders values('%1','%2','%3','%4','%5','%6','%7','%8')")
//                .arg(lineEdit_a).arg(lineEdit_b).arg(lineEdit_c).arg(lineEdit_d).arg(lineEdit_e).arg(lineEdit_f).arg(lineEdit_g).arg(lineEdit_h);
//        bool ret = query.exec(t_str);
//        qDebug()<<t_str;
//        DataBasePri::database.close();
//        if(ret == true){
//            QMessageBox msgBox;
//            msgBox.setWindowTitle("提示");
//            msgBox.setText("添加成功");
//            msgBox.exec();
//            updateDisplay();
//        }else{
//            QMessageBox msgBox;
//            msgBox.setWindowTitle("提示");
//            msgBox.setText("添加失败");
//            msgBox.exec();
//        }
//        updateDisplay();//刷新界面
//    }
//}

//void OrderWidget::on_pushButton_2_clicked()
//{
//    QString lineEdit_a = ui->lineEdit_a->text();//
//    QString lineEdit_b = ui->lineEdit_b->text();//
//    QString lineEdit_c = ui->lineEdit_c->text();//
//    QString lineEdit_d = ui->lineEdit_d->text();//
//    QString lineEdit_e = ui->lineEdit_e->text();//
//    QString lineEdit_f = ui->lineEdit_f->text();//
//    QString lineEdit_g = ui->lineEdit_g->text();//
//    QString lineEdit_h = ui->lineEdit_h->text();//
//    if(lineEdit_a == "" || lineEdit_b == ""){//判断输入情况
//        QMessageBox msgBox;
//        msgBox.setWindowTitle("提示");
//        msgBox.setText("用户名或密码不能为空");
//        msgBox.exec();
//    }else{
//        if (! DataBasePri::database.open()){//打开数据库
//            qCritical() << DataBasePri::database.lastError().text();
//            return;
//        }
//        QSqlQuery query(DataBasePri::database);
//        bool ret =  query.exec(QString::fromLocal8Bit("update orders set name = '%1' where id = '%2'").arg(lineEdit_b).arg(lineEdit_a));;
//        ret =  query.exec(QString::fromLocal8Bit("update orders set num = '%1' where id = '%2'").arg(lineEdit_c).arg(lineEdit_a));;
//        ret =  query.exec(QString::fromLocal8Bit("update orders set type = '%1' where id = '%2'").arg(lineEdit_d).arg(lineEdit_a));;
//        ret =  query.exec(QString::fromLocal8Bit("update orders set cost = '%1' where id = '%2'").arg(lineEdit_e).arg(lineEdit_a));;
//        ret =  query.exec(QString::fromLocal8Bit("update orders set price = '%1' where id = '%2'").arg(lineEdit_f).arg(lineEdit_a));;
//        ret =  query.exec(QString::fromLocal8Bit("update orders set remarks = '%1' where id = '%2'").arg(lineEdit_g).arg(lineEdit_a));;
//        ret =  query.exec(QString::fromLocal8Bit("update orders set supplier = '%1' where id = '%2'").arg(lineEdit_h).arg(lineEdit_a));;

//        DataBasePri::database.close();
//        if(ret == true){
//            QMessageBox msgBox;
//            msgBox.setWindowTitle("提示");
//            msgBox.setText("修改成功");
//            msgBox.exec();
//            updateDisplay();
//        }else{
//            QMessageBox msgBox;
//            msgBox.setWindowTitle("提示");
//            msgBox.setText("修改失败");
//            msgBox.exec();
//        }
//        updateDisplay();
//    }
//}

//void OrderWidget::on_pushButton_3_clicked()
//{
//    QTableWidgetItem *t_item = ui->tableWidget->item(selectrow,0);//当前选择哪一行
//    QString t_text = t_item->text();
//    qDebug()<<"t_item"<<t_text;
//    if (! DataBasePri::database.open()){//打开数据库
//        qCritical() << DataBasePri::database.lastError().text();
//        return;
//    }
//    QSqlQuery query(DataBasePri::database);//数据库请求对象
//    bool ret = query.exec(QString::fromLocal8Bit("delete from orders where id = '%1'").arg(t_text));
//    qDebug()<<ret;
//    if(ret == true){
//        QMessageBox msgBox;
//        msgBox.setWindowTitle("提示");
//        msgBox.setText("删除成功");
//        msgBox.exec();
//        updateDisplay();
//    }else{
//        QMessageBox msgBox;
//        msgBox.setWindowTitle("提示");
//        msgBox.setText("删除失败");
//        msgBox.exec();
//    }
//    DataBasePri::database.close();//关闭数据库
//    updateDisplay();//刷新界面
//}

//void OrderWidget::on_pushButton_4_clicked()
//{
//    emit sendDisWidget("m_AdminWidget");
//}

//目前添加逻辑有问题，添加老失败


#pragma execution_character_set("utf-8")
#include "database.h"
#include "orderwidget.h"
#include "ui_orderwidget.h"

OrderWidget::OrderWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OrderWidget)
{
    ui->setupUi(this);
    selectrow = 0;
    QStringList header;
    header <<"编号"<<"名称"<<"数量"<<"类型"<<"进货价"<<"指导价"<<"备注"<<"供应商";
    ui->tableWidget->setColumnCount(8);
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    ui->tableWidget->horizontalHeader()->setFixedHeight(50);

    ui->tableWidget->verticalHeader()->setHidden(true);
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(50);
    updateDisplay();
    connect(ui->tableWidget, SIGNAL(cellClicked(int,int)),this,SLOT(onHeaderClicked(int,int)));
}

OrderWidget::~OrderWidget()
{
    delete ui;
}

void OrderWidget::showEvent(QShowEvent *event)
{
    updateDisplay();
    ui->tableWidget->horizontalHeader()->resizeSection(0,ui->tableWidget->width()*0.1);
    ui->tableWidget->horizontalHeader()->resizeSection(1,ui->tableWidget->width()*0.1);
    ui->tableWidget->horizontalHeader()->resizeSection(2,ui->tableWidget->width()*0.1);
    ui->tableWidget->horizontalHeader()->resizeSection(3,ui->tableWidget->width()*0.1);
    ui->tableWidget->horizontalHeader()->resizeSection(4,ui->tableWidget->width()*0.1);
    ui->tableWidget->horizontalHeader()->resizeSection(5,ui->tableWidget->width()*0.1);
    ui->tableWidget->horizontalHeader()->resizeSection(6,ui->tableWidget->width()*0.2);
    ui->tableWidget->horizontalHeader()->resizeSection(7,ui->tableWidget->width()*0.2);

}

void OrderWidget::updateDisplay()
{
    ui->tableWidget->clearContents();
    QVector<QVector<QString>>  allinfo = DataBasePri::queryAll("orders");
    ui->tableWidget->setRowCount(allinfo.size());
    for(int i =0;i < allinfo.size();i++){
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(allinfo.at(i).at(0)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(allinfo.at(i).at(1)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(allinfo.at(i).at(2)));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(allinfo.at(i).at(3)));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(allinfo.at(i).at(4)));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(allinfo.at(i).at(5)));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(allinfo.at(i).at(6)));
        ui->tableWidget->setItem(i,7,new QTableWidgetItem(allinfo.at(i).at(7)));
    }
}

void OrderWidget::onHeaderClicked(int row,int column)
{
    ui->lineEdit_a->setText(ui->tableWidget->item(row,0)->text());
    ui->lineEdit_b->setText(ui->tableWidget->item(row,1)->text());
    ui->lineEdit_c->setText(ui->tableWidget->item(row,2)->text());
    ui->lineEdit_d->setText(ui->tableWidget->item(row,3)->text());
    ui->lineEdit_e->setText(ui->tableWidget->item(row,4)->text());
}

void OrderWidget::on_pushButton_1_clicked()
{
    QString lineEdit_a = ui->lineEdit_a->text();//
    QString lineEdit_b = ui->lineEdit_b->text();//
    QString lineEdit_c = ui->lineEdit_c->text();//
    QString lineEdit_d = ui->lineEdit_d->text();//
    QString lineEdit_e = ui->lineEdit_e->text();//
    QString lineEdit_f = ui->lineEdit_f->text();//
    QString lineEdit_g = ui->lineEdit_g->text();//
    QString lineEdit_h = ui->lineEdit_h->text();//
    if(lineEdit_a == "" || lineEdit_b == ""){//判断是否为空
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("用户名或密码不能为空");
        msgBox.exec();
    }else{
        if (! DataBasePri::database.open()){//打开数据库
            qCritical() << DataBasePri::database.lastError().text();
            return;
        }
        QSqlQuery query(DataBasePri::database);//创建数据库请求对象
        QString t_str = QString::fromLocal8Bit("insert into orders values('%1','%2','%3','%4','%5','%6','%7','%8')")
                .arg(lineEdit_a).arg(lineEdit_b).arg(lineEdit_c).arg(lineEdit_d).arg(lineEdit_e).arg(lineEdit_f).arg(lineEdit_g).arg(lineEdit_h);
        bool ret = query.exec(t_str);
        qDebug()<<t_str;
        DataBasePri::database.close();
        if(ret == true){
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("添加成功");
            msgBox.exec();
            updateDisplay();
        }else{
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("添加失败");
            msgBox.exec();
        }
        updateDisplay();//刷新界面
    }
}

void OrderWidget::on_pushButton_2_clicked()
{
    QString lineEdit_a = ui->lineEdit_a->text();//
    QString lineEdit_b = ui->lineEdit_b->text();//
    QString lineEdit_c = ui->lineEdit_c->text();//
    QString lineEdit_d = ui->lineEdit_d->text();//
    QString lineEdit_e = ui->lineEdit_e->text();//
    QString lineEdit_f = ui->lineEdit_f->text();//
    QString lineEdit_g = ui->lineEdit_g->text();//
    QString lineEdit_h = ui->lineEdit_h->text();//
    if(lineEdit_a == "" || lineEdit_b == ""){//判断输入情况
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("用户名或密码不能为空");
        msgBox.exec();
    }else{
        if (! DataBasePri::database.open()){//打开数据库
            qCritical() << DataBasePri::database.lastError().text();
            return;
        }
        QSqlQuery query(DataBasePri::database);
        bool ret =  query.exec(QString::fromLocal8Bit("update orders set name = '%1' where id = '%2'").arg(lineEdit_b).arg(lineEdit_a));;
        ret =  query.exec(QString::fromLocal8Bit("update orders set num = '%1' where id = '%2'").arg(lineEdit_c).arg(lineEdit_a));;
        ret =  query.exec(QString::fromLocal8Bit("update orders set type = '%1' where id = '%2'").arg(lineEdit_d).arg(lineEdit_a));;
        ret =  query.exec(QString::fromLocal8Bit("update orders set cost = '%1' where id = '%2'").arg(lineEdit_e).arg(lineEdit_a));;
        ret =  query.exec(QString::fromLocal8Bit("update orders set price = '%1' where id = '%2'").arg(lineEdit_f).arg(lineEdit_a));;
        ret =  query.exec(QString::fromLocal8Bit("update orders set remarks = '%1' where id = '%2'").arg(lineEdit_g).arg(lineEdit_a));;
        ret =  query.exec(QString::fromLocal8Bit("update orders set supplier = '%1' where id = '%2'").arg(lineEdit_h).arg(lineEdit_a));;

        DataBasePri::database.close();
        if(ret == true){
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("修改成功");
            msgBox.exec();
            updateDisplay();
        }else{
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("修改失败");
            msgBox.exec();
        }
        updateDisplay();
    }
}

void OrderWidget::on_pushButton_3_clicked()
{
    QTableWidgetItem *t_item = ui->tableWidget->item(selectrow,0);//当前选择哪一行
    QString t_text = t_item->text();
    qDebug()<<"t_item"<<t_text;
    if (! DataBasePri::database.open()){//打开数据库
        qCritical() << DataBasePri::database.lastError().text();
        return;
    }
    QSqlQuery query(DataBasePri::database);//数据库请求对象
    bool ret = query.exec(QString::fromLocal8Bit("delete from orders where id = '%1'").arg(t_text));
    qDebug()<<ret;
    if(ret == true){
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("删除成功");
        msgBox.exec();
        updateDisplay();
    }else{
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("删除失败");
        msgBox.exec();
    }
    DataBasePri::database.close();//关闭数据库
    updateDisplay();//刷新界面
}

void OrderWidget::on_pushButton_4_clicked()
{
    emit sendDisWidget("m_AdminWidget");
}
